<?php

namespace App\Console\Commands;

use App\Jobs\Send;
use App\Lead;
use App\Notifications\JpushMsg;
use App\Project;
use App\ProjectUser;
use App\User;
use App\UserProject;
use App\Wechat;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class WechatAdCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wechat:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $project_id=24;
        $create_user_id=9;
        $app=\EasyWeChat::officialAccount();
        $accessToken=$app->access_token->getToken();
        $client=new Client();
        $next=true;
        $page=1;
        while ($next){
            $result=$client->get('https://api.weixin.qq.com/marketing/wechat_ad_leads/get',[
                'query'=>[
                    'page'=>$page,
                    'access_token'=>$accessToken['access_token'],
                    'version'=>'v1.0'
                ]
            ]);
            $result=json_decode($result->getBody()->getContents(),true);
            if ($result['errcode']==0){
                $list=$result['data']['list'];
                if (!empty($list)){



                    foreach ($list as $item){
                       $wechat= Wechat::whereClickId($item['click_id'])->first();
                       if (!$wechat){
                           \Log::info('新增线索',$item);
                        $wechat=   new Wechat();
                        $wechat->adgroup_id=$item['adgroup_id'];
                        $wechat->adgroup_name=$item['adgroup_name'];
                        $wechat->campaign_id=$item['campaign_id'];
                        $wechat->campaign_name=$item['campaign_name'];
                        $wechat->click_id=$item['click_id'];
                        $wechat->leads_info=$item['leads_info'];
                        $wechat->agency_id=$item['agency_id'];
                        $wechat->agency_name=$item['agency_name'];
                        $wechat->save();
                           $name = collect($item['leads_info'])->firstWhere('key', '=', '姓名')['value'];
                           $mobile = collect($item['leads_info'])->firstWhere('key', '=', '电话号码')['value'];
                           if (!$mobile){
                               $mobile = collect($item['leads_info'])->firstWhere('key', '=', '电话')['value'];
                           }
                           $area = collect($item['leads_info'])->firstWhere('key', '=', '需求面积')['value'];
                           $lead = new Lead();
                           $lead->customer_id = 0;
                           $lead->is_transform = 0;
                           if ($name) {
                               $lead->name = $name;
                           }
                           if ($mobile) {
                               $lead->mobile = $mobile;
                           }
                           if ($area) {
                               $lead->crm_vfnlcv = $area;
                           }
                           $lead->source = '微信朋友圈';

                           $project = Project::find($project_id);
                           if ($project->type == 0) {
                               $userProject = new  UserProject();
                               $owner_user_id = $userProject->current($project_id);
                           } elseif ($project->type == 1) {
                               $userProject = new ProjectUser();
                               $owner_user_id = $userProject->current($project_id);
                           } else {
                               $owner_user_id = 1;
                           }
                           $lead->create_user_id = $create_user_id;
                           $lead->owner_user_id = $owner_user_id;
                           $lead->next_time = 0;
                           $lead->create_time = time();
                           $lead->status = '潜在客户';
                           $lead->project_id = $project_id;
                           $lead->update_time = time();
                           $lead->save();
                           $jpush = new JpushMsg();
                           $jpush->setContent('新增一条线索');
                           $jpush->setTitle('新增一条线索');
                          try{
                               if ($owner_user_id==1){
                                   dispatch(new Send($lead));
                               }else{
                                   $user=  User::find($owner_user_id);
                                   if ($user){
                                       $user->notify(new \App\Notifications\LeadsNotification($lead));
                                   }
                               }
                           }catch (\Exception $exception){
                           }
                           \Log::info('新增线索', $lead->toArray());
                       }
                    }
                }
            }
            if ($result['data']['page_info']['total_page']<=$page){
                $next=false;
            }
            $page++;
        }

    }
}
